package com.douban.crawl.crawler;


import com.douban.crawl.db.BookDao;
import com.douban.crawl.model.Book;
import com.douban.crawl.utils.CommonUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class BookCrawler {
    public static void main(String[] args) throws IOException, InterruptedException {
        CommonUtil.setGlobalIPAndPort();
        String header = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36";
        String url = "https://book.douban.com/top250";
        String pageInfo = "?start=";
        for (int i = 0; i <= 9; i++) {
            String newUrl = url + pageInfo + (25)*i;
            extractPage(newUrl, header);
            // 休息200ms
            TimeUnit.MILLISECONDS.sleep(200);
        }
    }
    
    public static void extractPage(String url, String header) throws IOException {
        BookDao bookDao = new BookDao();

        Document doc = Jsoup.connect(url).header("User-Agent", header).get();
        Element content = doc.body().getElementById("content");
        Elements itemList = content.getElementsByClass("item");
        for (Element itemEle : itemList) {
            // id
            String bookId = CommonUtil.extractNumber(itemEle.getElementsByTag("a").get(0).attr("href"));
            // 图片链接
            String imgUrl = itemEle.getElementsByTag("a").get(0).getElementsByTag("img").attr("src");
            // 图书名
            String bookName = itemEle.getElementsByClass("pl2").get(0).getElementsByTag("a").text();
            // 图书信息
            String bookInfo = itemEle.getElementsByClass("pl").get(0).text();
            // 评分
            String score = itemEle.getElementsByClass("rating_nums").get(0).text();
            // 描述
            String description = "";
            Elements inqs = itemEle.getElementsByClass("inq");
            if (inqs != null && inqs.size() > 0) {
                description = inqs.get(0).text();
            }

            StringBuilder res = new StringBuilder();
            res.append("id:").append(bookId).append(System.lineSeparator())
                    .append("图片链接:").append(imgUrl).append(System.lineSeparator())
                    .append("图书名:").append(bookName).append(System.lineSeparator())
                    .append("图书信息:").append(bookInfo).append(System.lineSeparator())
                    .append("评分:").append(score).append(System.lineSeparator())
                    .append("描述:").append(description).append(System.lineSeparator());
            System.out.println(res);

            Book book = new Book(bookName, bookInfo, score, description, bookId, imgUrl);

            bookDao.save(book);
        }
    }
}
